
          /*
           -------ProductDao By Raj Vallamsetla------
  		   
				For the insertion of Base Product 

          */

package com.scalar.chiptrack.operations.process.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.scalar.ScalarException;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.db.DataSource;
import com.scalar.chiptrack.operations.process.forms.AssemblyProcessSplitForm;
import com.scalar.chiptrack.utils.DBLoader;
import com.scalar.chiptrack.utils.SqlQueryMappings;

public class AssemblyProcessSplitDAO 
{

 private static DBLoader s_dbLoader = null;

 
     public AssemblyProcessSplitDAO()
	  {
	
	  }


 public String save(DBTransaction dbTransaction,String fabTrackId,String productName,String status) throws ScalarException {	
		
		String assemblyTrackId = "0";
		Connection conn = null;
		ResultSet rs    = null;
		PreparedStatement pstmt=null;
        

		try{
				
				
		
		if (dbTransaction == null) {
                            conn = DataSource.getConnection();
                        }
                        else {
                            conn = dbTransaction.getConnection();
                        }	

		pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.insertAssemblytrack"));					
		pstmt.setString(1,fabTrackId);
		pstmt.setString(2,productName);
		pstmt.setString(3,status);

		pstmt.executeUpdate();

		pstmt.close();
		pstmt = conn.prepareStatement(SqlQueryMappings.get("operations.process.upload.readIdentity"));
		rs = pstmt.executeQuery();

		while(rs.next()){
				assemblyTrackId = rs.getString("docID");
			}
		
		}

		catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not save Assembly Process Split database", sqle);
        }

			
			finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
        }

			return assemblyTrackId;

	
		}

	
public String getAssembly(String trackId) throws ScalarException{


				 Connection conn = null;
				 ResultSet rs    = null;
				 PreparedStatement pstmt=null;
				 String checkAssembly = "";
                

		
		try{
				
		
        conn = DataSource.getConnection();                    
		//System.out.println("Conection "+conn);
		pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.selectAssembly"));
		pstmt.setString(1,trackId);
		rs=pstmt.executeQuery();

		while(rs.next()){
		checkAssembly ="exists";		     
		}


	}
			catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not getAssembly from database", sqle);
        }


				finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
        }
			
			return checkAssembly;
		
        }
	


	public void deleteAssembly(String productId) throws ScalarException{


				 Connection conn = null;
				 ResultSet rs    = null;
				 PreparedStatement pstmt=null;
          
		try{
				
		conn = DataSource.getConnection();
     
		pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.assemblyProcessDelete"));
		pstmt.setString(1,productId);
		pstmt.executeUpdate();

		conn.commit();     
	
	}
			catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not save FabProcess SetUp from database", sqle);
        }


				finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
        }
		
        }




		public void delete(String pageId){


				 Connection conn = null;
				 ResultSet rs    = null;
				 PreparedStatement pstmt=null;
                 
		try{
				
								  
            if (s_dbLoader == null) {
                s_dbLoader = DBLoader.getInstance();
            }

         conn = s_dbLoader.getConnection();
		 pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.fabProcessDeleteField"));
		 pstmt.setString(1,pageId);
		 pstmt.executeUpdate();
		 conn.commit();
			  
		 }

	    	catch (Exception sqle) {
            sqle.printStackTrace();
            //throw new ScalarException ("Could not save FabProcess SetUp from database", sqle);
        }


            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore){}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore){}
            }
        }
		

	/* --- 
	
	Method Added by Raj Vallamsetla on 07/15 checkAssemblyTrackId

	*/
	public AssemblyProcessSplitForm getAssemblyTrackId(String [] fabTrackId)	throws ScalarException
    {
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs     = null;
		String trackId   = "";
		String assyName	  = "";
		String check	  = "";
		ArrayList assyTrackList= new ArrayList();
		ArrayList assyProductList=new ArrayList(); 

		AssemblyProcessSplitForm assemblyProcessSplitForm = new AssemblyProcessSplitForm();
				 
		try{
				
		   
		   conn = DataSource.getConnection();
		   	
		   for (int i=0;i<fabTrackId.length;i++){
           pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.assemblyTrackId"));
		   pstmt.setString(1,fabTrackId[i]);
		   rs = pstmt.executeQuery();
		
		

			while(rs.next())
			{
				  trackId = rs.getString(1);
				  assyName = rs.getString(2);
				  check="exists";
				  assyTrackList.add(trackId);
				  assyProductList.add(assyName);	
				 
			}
			pstmt.close();
			pstmt=null;
		   }
 
			if(check.equals(""))
				assemblyProcessSplitForm = null;
		
						
			if(assemblyProcessSplitForm!=null){
							
				int len =  assyTrackList.size();
				String [] assyTrackId = new String [len]	;
				String [] assyNameList = new String [len];

				

				for(int i=0;i<assyTrackList.size();i++){
				
					assyTrackId[i]=(String)assyTrackList.get(i);	
					assyNameList[i]=(String)assyProductList.get(i);


				}
				assemblyProcessSplitForm.setAssemblyTrackId(assyTrackId);
				assemblyProcessSplitForm.setAssemblyProduct(assyNameList);

			
				
			}
    
		   }catch(Exception e){
				e.printStackTrace();
				System.out.println("debug5") ;
				throw new ScalarException("tracking Fab failed ", e);
		   }finally {
				if (rs != null) {
					try { rs.close(); } catch (SQLException ignore) {System.out.println("RS in getFabTrakID() = "+ignore.getMessage());}
				}
				if (pstmt != null) {
					try { pstmt.close(); } catch (SQLException ignore) {System.out.println("PSTMT in getFabTrakID() = "+ignore.getMessage());}
				}
				if (conn != null) {
					try { conn.close(); } catch (SQLException ignore) {System.out.println("Conn in getFabTrakID() = "+ignore.getMessage());}
				}
        }

			return assemblyProcessSplitForm;

} 






}	